package com.xsd.prc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 流式计算demo
 * @Author: zhoufuzong
 * @Created: 2021-01-06 20:02:50
 * @Version: 1.1.0
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
class User{
    private Integer id;
    private String userName;
    private int age;


}

/**
 * 题目： 按照给出的数据，找出同时满足以下条件的用户，也即以下条件全部满足
 *  *   偶数ID ，且年龄大于24，且用户名转为大写且用户名字母排倒叙
 *  *   只输出一个用户名字
 */
public class StreamDemo {
    public static void main(String[] args) {
        User u1 = new User(11,"a",23);
        User u2 = new User(12,"b",24);
        User u3 = new User(13,"c",22);
        User u4 = new User(14,"d",28);
        User u5 = new User(16,"e",26);

        List<User> list = Arrays.asList(u1,u2,u3,u4,u5);


        long start = System.nanoTime();
        list.stream()
                .filter(user -> user.getId()%2==0)
                .filter(t->t.getAge()>24)
                .map(t->t.getUserName().toUpperCase())
                .sorted(((o1, o2) -> o2.compareTo(o1)))
                .limit(1)
                .forEach(System.out::println)
                ;
        long end =System.nanoTime();
        System.out.println(end-start);


        // 并行流
        long start1 = System.nanoTime();
        list.parallelStream()
                .filter(user -> user.getId()%2==0)
                .filter(t->t.getAge()>24)
                .map(t->t.getUserName().toUpperCase())
                .sorted(((o1, o2) -> o2.compareTo(o1)))
                .limit(1)
                .forEach(System.out::println);

        long end1 =System.nanoTime();

        System.out.println(end1-start1);


    }
}
